package com.example.menuvirtualclient.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.menuvirtualclient.R;
import com.example.menuvirtualclient.core.Menu;
import com.example.menuvirtualclient.presenter.MenuPresenter;

public class MenuAdapter extends ArrayAdapter<Menu> {
	Context context;
	int layoutResourceId;
	List<Menu> objects = null;
	float imageWidth;
	private MenuPresenter _presenter;
	

	public MenuAdapter(Context context, int layoutResourceId, List<Menu> objects) {
		super(context, layoutResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.objects = objects;
		
	}
	
	public MenuAdapter(Context context, int layoutResourceId, List<Menu> objects, MenuPresenter presenter) {
		super(context, layoutResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.objects = objects;
		_presenter = presenter;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ComponentsHolder holder;
		final int cc = position;

		// row = null; // This clears the view, so the rows are well drawn but
		// makes the app much slower

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);
//			row.setTag(tag)
			holder = new ComponentsHolder();
			holder.id = (TextView) row.findViewById(R.id.tvSeccionId);
			holder.name = (TextView) row.findViewById(R.id.tvSeccionName);
			holder.description = (TextView) row.findViewById(R.id.tvSeccionDescr);

			holder.name.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					((View)v.getParent()).performClick();
//					_presenter.LoadMenu(Integer.parseInt(holder.id.getText().toString()));					
				}
			});
			
			holder.description.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					((View)v.getParent()).performClick();
//					_presenter.LoadMenu(Integer.parseInt(holder.id.getText().toString()));					
				}
			});
			
			
			row.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//_presenter.LoadMenu(Integer.parseInt(holder.id.getText().toString()));	
//					_presenter.LoadDishes(Integer.parseInt(holder.id.getText().toString()));
					_presenter.LoadDishes(Integer.parseInt(((TextView)(v.findViewById(R.id.tvSeccionId))).getText().toString()));
					((ImageButton)((View)v.getParent().getParent().getParent()).findViewById(R.id.btnOptionsLeft)).performClick();
				}
			});

			row.setTag(holder);
		} else {
			holder = (ComponentsHolder) row.getTag();

			holder.id.setText("");
			holder.name.setText("");
			holder.description.setText("");

		}
		// agrego swipe
		row.setOnTouchListener(new OnSwipeTouchListener() {
		    
		});

		Menu componentsList = objects.get(cc);

		holder.id.setText((String.valueOf(componentsList.getId())));
		holder.name.setText(componentsList.getName());
		holder.description.setText(componentsList.getDescription());
		

		return row;
	}

	static class ComponentsHolder {
		TextView id;
		TextView name;
		TextView description;
		
	}

	// resize the image proportionately so it fits the entire space
	@SuppressWarnings("unused")
	private void setImageBitmap(Integer item, ImageView imageView) {
		Bitmap bitmap = BitmapFactory.decodeResource(getContext()
				.getResources(), item);
		float i = ((float) imageWidth) / ((float) bitmap.getWidth());
		float imageHeight = i * (bitmap.getHeight());
		FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView
				.getLayoutParams();
		params.height = (int) imageHeight;
		params.width = (int) imageWidth;
		imageView.setLayoutParams(params);
		imageView.setImageResource(item);
	}

	public static float convertDpToPixel(float dp, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return px;
	}
}
